//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;
using LitwareHR.Portfolio.Contract;
using LitwareHR.Portfolio.DataModel.Sitka.Tests.Mocks;
using Shp.Runtime.DataModel.Sitka;
using Shp.Runtime.Contract;
using Shp.Runtime.Sitka.ServiceAgent;
using Shp.Runtime.Sitka.ServiceAgent.Entities;
using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;
using System.Collections.ObjectModel;
using LitwareHR.Recruiting.Contract;
using LitwareHR.Recruiting.Services;

namespace LitwareHR.Portfolio.DataModel.Sitka.Tests
{
	
	/// <summary>
	/// Summary description for DeploymentDemoFixture
	/// </summary>
	[TestClass]
	public class DeploymentDemoFixture
	{
		const string MockDefNamePosition = "position";
		public DeploymentDemoFixture()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		#region Additional test attributes
		//
		// You can use the following additional attributes as you write your tests:
		//
		// Use ClassInitialize to run code before running the first test in the class
		// [ClassInitialize()]
		// public static void MyClassInitialize(TestContext testContext) { }
		//
		// Use ClassCleanup to run code after all tests in a class have run
		// [ClassCleanup()]
		// public static void MyClassCleanup() { }
		//
		// Use TestInitialize to run code before running each test 


		//
		// Use TestCleanup to run code after each test has run
		// [TestCleanup()]
		// public void MyTestCleanup() { }
		//
		#endregion

		[TestMethod]
		public void ShouldInsertDataDemo()
		{
			ProvisioningRequest request = new ProvisioningRequest();
            string nroTenant= ConfigurationManager.AppSettings["TenantNumber"].ToString();
			string tenant1 = "contoso"+nroTenant;
			string tenant2 = "fabrikam"+nroTenant;

            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove("TenantNumber");
            config.AppSettings.Settings.Add("TenantNumber", (Convert.ToInt32(nroTenant) + 1).ToString());
            config.Save();
            
			request.TenantAlias = tenant1;
			request.TenantName = "Contoso Inc.";
			request.DomainModel = 0;
			request.AdminPassword = "Passw0rd!";

			LitwareHR.Portfolio.Gateways.ProvisioningGateway.DoProvisioning(request);

			ProvisioningRequest request2 = new ProvisioningRequest();
			request2.TenantAlias = tenant2;
			request2.TenantName = "Fabrikam Inc.";
			request2.DomainModel = 0;
			request2.AdminPassword = "Passw0rd!";

			LitwareHR.Portfolio.Gateways.ProvisioningGateway.DoProvisioning(request2);

			//Get data
            Guid guidContoso = LitwareHR.Portfolio.Gateways.SystemGateway.GetTenantId(tenant1);
            Guid guidFabrikam = LitwareHR.Portfolio.Gateways.SystemGateway.GetTenantId(tenant2);

			Assert.IsNotNull(guidContoso);
			Assert.IsNotNull(guidFabrikam);

			//Insert Positions
			RepositoryFactory factoryContoso = new RepositoryFactory(guidContoso);
			BaseDataModel dataModelContoso = new BaseDataModel(factoryContoso);

            RepositoryFactory factoryFabrikam = new RepositoryFactory(guidFabrikam);
            BaseDataModel dataModelFabrikam = new BaseDataModel(factoryFabrikam);

            List<EntityField> entities = new List<EntityField>();
			
			EntityField entityField = new EntityField();
			entityField.Name = "code";
			entityField.Value = "5432";
			entityField.TypeId = new Guid("fa564e7c-f59b-480f-a768-a1d02a34e32c");

			entities.Add(entityField);

			EntityField entityField2 = new EntityField();
			entityField2.Name = "location";
			entityField2.Value = "Peru 375";
			entityField2.IsShared = true;
			entityField2.TypeId = new Guid("b44a04ca-3426-4c8d-9fc0-431de22d6f4e");

			entities.Add(entityField2);
			
			Guid instance1 = dataModelContoso.AddInstance(guidContoso, MockDefNamePosition, entities.ToArray());
			Guid instance2 = dataModelFabrikam.AddInstance(guidFabrikam, MockDefNamePosition, entities.ToArray());

            Assert.IsNotNull(instance1);
            Assert.IsNotNull(instance2);

            Collection<LitwareHR.Recruiting.Contract.Field> entityList = new Collection<LitwareHR.Recruiting.Contract.Field>();
            foreach (EntityField field in entities)
            {
                entityList.Add(new LitwareHR.Recruiting.Contract.Field(field.Name,field.Value));
            }
            
            Position position1 = new Position(instance1, entityList);
            position1.TenantId = guidContoso;

            Position position2 = new Position(instance2, entityList);
            position2.TenantId = guidFabrikam;

            Guid resultPosition1 = ProcessLogic.OpenRecruitingProcess(position1);
            Guid resultPosition2 = ProcessLogic.OpenRecruitingProcess(position2);

            Assert.IsNotNull(resultPosition1);
            Assert.IsNotNull(resultPosition2);

            //Insert Resumes
			string LitwareHrKey = ConfigurationManager.AppSettings["LitwareContainerKey"];
			
			DataInitialize dataInitialize = new DataInitialize(guidContoso.ToString(), LitwareHrKey);
			Guid resume1 = Guid.NewGuid();
			Guid resume2 = Guid.NewGuid();
            Guid resume3 = Guid.NewGuid();
            dataInitialize.CreateResumeInstance(resume1, "Info", "Redmond", "John", "Hank", "4444-4444", DateTime.Parse("2008-01-01"), resultPosition1, "admin@contoso", false, false);
            dataInitialize.CreateResumeInstance(resume3, "Info Aux", "New York", "Bruce", "Hook", "5555-4444", DateTime.Today.AddDays(-25), resultPosition1, "bhook@contoso", false, false);

			DataInitialize dataInitialize2 = new DataInitialize(guidFabrikam.ToString(), LitwareHrKey);

            dataInitialize2.CreateResumeInstance(resume2, "Info Additional", "Redmond", "Joe", "Hank1", "5555-5555", DateTime.Parse("2007-12-30"), resultPosition2, "admin@fabrikam", false, false);

			using (Repository<GenericExtensibleEntity> repository = factoryContoso.GetDataRepository(guidContoso))
			{
				GenericExtensibleEntity resumeRetrieved = repository.GetById(resume1);
				Assert.IsNotNull(resumeRetrieved);
				Assert.AreEqual("Info", resumeRetrieved.GetFieldValue("AdditionalInfo"));
				Assert.AreEqual("Redmond", resumeRetrieved.GetFieldValue("Address"));
				Assert.AreEqual("John", resumeRetrieved.GetFieldValue("ApplicantName"));
				Assert.AreEqual("Hank", resumeRetrieved.GetFieldValue("ApplicantSurname"));
				Assert.AreEqual("4444-4444", resumeRetrieved.GetFieldValue("PhoneNumber"));
				Assert.AreEqual("admin@contoso", resumeRetrieved.GetFieldValue("Upn"));
				Assert.AreEqual(false, bool.Parse(resumeRetrieved.GetFieldValue("IsVideoAvailable")));
			}

            using (Repository<GenericExtensibleEntity> repository = factoryContoso.GetDataRepository(guidContoso))
            {
                GenericExtensibleEntity resumeRetrieved = repository.GetById(resume3);
                Assert.IsNotNull(resumeRetrieved);
                Assert.AreEqual("Info Aux", resumeRetrieved.GetFieldValue("AdditionalInfo"));
                Assert.AreEqual("New York", resumeRetrieved.GetFieldValue("Address"));
                Assert.AreEqual("Bruce", resumeRetrieved.GetFieldValue("ApplicantName"));
                Assert.AreEqual("Hook", resumeRetrieved.GetFieldValue("ApplicantSurname"));
                Assert.AreEqual("5555-4444", resumeRetrieved.GetFieldValue("PhoneNumber"));
                Assert.AreEqual("bhook@contoso", resumeRetrieved.GetFieldValue("Upn"));
                Assert.AreEqual(false, bool.Parse(resumeRetrieved.GetFieldValue("IsVideoAvailable")));
            }

			using (Repository<GenericExtensibleEntity> repository = factoryContoso.GetDataRepository(guidFabrikam))
			{
				GenericExtensibleEntity resumeRetrieved2 = repository.GetById(resume2);
				Assert.IsNotNull(resumeRetrieved2);
				Assert.AreEqual("Info Additional", resumeRetrieved2.GetFieldValue("AdditionalInfo"));
				Assert.AreEqual("Redmond", resumeRetrieved2.GetFieldValue("Address"));
				Assert.AreEqual("Joe", resumeRetrieved2.GetFieldValue("ApplicantName"));
				Assert.AreEqual("Hank1", resumeRetrieved2.GetFieldValue("ApplicantSurname"));
				Assert.AreEqual("5555-5555", resumeRetrieved2.GetFieldValue("PhoneNumber"));
				Assert.AreEqual("admin@fabrikam", resumeRetrieved2.GetFieldValue("Upn"));
				Assert.AreEqual(false, bool.Parse(resumeRetrieved2.GetFieldValue("IsVideoAvailable")));
			}
            global::System.Windows.Forms.MessageBox.Show("tenants: contoso"+nroTenant+" and fabrikam"+nroTenant);
		}		
	}
}
